﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PrjAssessModels
{
    public class DataConvert<TChild, TParent> where TChild : TParent, new()
    {
        public static void ParsePropterty(TParent source,TParent desitination)
        {
            var ParentType = typeof(TParent);
            var Properties = ParentType.GetProperties();
            foreach (var Propertie in Properties)
            {
                if (Propertie.Name == "Id")
                {
                    continue;
                }
                //循环遍历属性
                if (Propertie.CanRead && Propertie.CanWrite)
                {
                    try
                    {
                        //进行属性拷贝
                        Propertie.SetValue(desitination, Propertie.GetValue(source, null), null);
                    }
                    catch
                    {
                        continue;
                    }
                }
            }
        }

        public static TChild AutoCopy(TParent parent)
        {
            TChild child = new TChild();
            var ParentType = typeof(TParent);
            var Properties = ParentType.GetProperties();
            foreach (var Propertie in Properties)
            {
                //循环遍历属性
                if (Propertie.CanRead && Propertie.CanWrite)
                {
                    //进行属性拷贝
                    Propertie.SetValue(child, Propertie.GetValue(parent, null), null);
                }
            }
            return child;
        }
    }
}
